<?php
session_start();
include("includes/session.php");
if($do == 'list'){
	$search = '';
	if((isset($_REQUEST['s']))&&($_REQUEST['s']!='')){
		$search = $_REQUEST['s'];
	}
	elseif(isset($_REQUEST['search'])){
		$search = $_REQUEST['search'];
	}
	$page = $_REQUEST['page'];
	$result = mysql_query("SELECT COUNT(*) FROM `contract` WHERE `name` LIKE '%$search%' OR `tax` LIKE '%$search%' OR `address` LIKE '%$search%' OR `contract` LIKE '%$search%'");
	$total = @mysql_result($result, 0, 0);  
	$limit = mysql_result(mysql_query("SELECT `value` FROM `setting` WHERE `name` = 'contract_display'"),0,0);
	$pager = Pager::getPagerData($total, $limit, $page);
	$offset = $pager->offset;
	$limit = $pager->limit;
	$page = $pager->page;
	if($offset >= 0){
		$contract_sql = mysql_query("SELECT * FROM `contract` WHERE `name` LIKE '%$search%' OR `tax` LIKE '%$search%' OR `address` LIKE '%$search%' OR `contract` LIKE '%$search%' ORDER BY `date` DESC, `id` DESC LIMIT $offset, $limit");
	}
	else{
		$contract_sql = mysql_query("SELECT * FROM `contract` WHERE `name` LIKE '%$search%' OR `tax` LIKE '%$search%' OR `address` LIKE '%$search%' OR `contract` LIKE '%$search%' ORDER BY `date` DESC, `id` DESC");
	}
	echo mysql_error();
	$contract = '';
	while($contract_rs = mysql_fetch_object($contract_sql)){
		$tax_trim = '';
		$tax_ex = array_map('trim',explode(" ",$contract_rs->tax));
		for($i=0;$i<=20;$i++){
			$tax_trim = trim($tax_trim.$tax_ex[$i]);
		}
		$rsid = $contract_rs->id;
		$vat_form[0] = '';
		$vat_form = str_split($contract_rs->form, 6);
		switch($vat_form[0]){
			case "01GTKT":	$vat_type = 'HÓA ĐƠN GIÁ TRỊ GIA TĂNG';break;
			case "02GTTT":	$vat_type = 'HÓA ĐƠN BÁN HÀNG';break;
			case "03XKNB":	$vat_type = 'PHIẾU XUẤT KHO KIÊM VẬN CHUYỂN HÀNG HÓA NỘI BỘ';break;
			case "04HGDL":	$vat_type = 'PHIẾU XUẤT KHO GỬI BÁN HÀNG ĐẠI LÝ';break;
			case "06HDXK":	$vat_type = 'HÓA ĐƠN XUẤT KHẨU';break;
			case "07KPTQ":	$vat_type = 'HÓA ĐƠN BÁN HÀNG(PHI THUẾ QUAN)';break;
			default : 	$vat_01GTKT = $vat_02GTTT = $vat_03XKNB = $vat_04HGDL = $vat_06HDXK = $vat_07KPTQ = '';break;
		}
		$time = date('d/m/Y', $contract_rs->date);
		$usdata = mysql_fetch_object(mysql_query("SELECT * FROM `user` where `username` = '$contract_rs->userlog'"));
		$sql_product = mysql_query("SELECT * FROM `product` where `cid` = '$rsid' order by `order` asc");
		$contract_date = date('d-m-Y', $contract_rs->date);
		$contract_status_next = $contract_rs->status + 1;
		$contract .= '~>'.$contract_rs->id.'~>'.$contract_rs->name.'~>'.$contract_rs->tax.'~>'.$contract_rs->address.'~>'.$contract_rs->director.'~>'.$contract_rs->position.'~>'.$contract_date.'~>'.$contract_rs->contract.'~>'.$contract_rs->datelog.'~>'.$usdata->fullname.'~>'.$contract_rs->datebb.'~>'.$contract_rs->product.'~>'.$contract_rs->niemphong.'~>'.$contract_rs->tamung.'~>'.$contract_rs->ngaygiao.'~>'.$contract_rs->marketing.'~>'.$contract_rs->note.'~>'.contract_status($contract_rs->status).'~>'.contract_status_class($contract_rs->status).'~>'.contract_status_class($contract_status_next);
		$product_stt = 0;
		$product_total = 0;
		while($rs_product = mysql_fetch_object($sql_product)){
			$product_stt++;
			$product_amount = $rs_product->price * $rs_product->quantity;
			$product_total = $product_total + $product_amount;
			$product .= '~>'.$product_stt.'~>'.$rs_product->cid.'~>'.$rs_product->form.'~>'.$rs_product->serial.'~>'.number_format($rs_product->quantity).'~>'.unit($rs_product->unit).'~>'.$rs_product->product_name.'~>'.$rs_product->product_page.'~>'.number_format($rs_product->price).'~>'.$rs_product->color.'~>'.bowel($rs_product->bowel).'~>'.$rs_product->size.'~>'.$rs_product->signed.'~>'.$rs_product->sohuy.'~>'.$rs_product->banhuy.'~>'.qty_format($rs_product->quantity_t).'~>'.qty_format($rs_product->quantity_d).'~>'.$rs_product->hoavan.'~>'.$rs_product->logo.'~>'.$rs_product->matma.'~>'.$rs_product->order.'~>'.number_format($product_amount).'~>'.$rs_product->unit;
		}
		$product_vat = $product_total / 10;
		$product_grand = round($product_total + $product_vat);
		$product_word = Docso::doc($product_grand);
		$product_money .= '~>'.$rsid.'~>'.number_format($product_total).'~>'.number_format($product_vat).'~>'.number_format($product_grand).'~>'.$product_word;
	}
	$contract_from = $offset + 1;
	$contract_to = $offset + $limit;
	$itt->assign("contract", arrchunk($contract, 20));
	$itt->assign("product", arrchunk($product, 23));
	$itt->assign("product_money", arrchunk($product_money, 5));
	$itt->assign("contract_total", $total);
	$itt->assign("contract_from", $contract_from);
	if($contract_to < $total){
		$itt->assign("contract_to", $contract_to);
	}
	else{
		$itt->assign("contract_to", $total);
	}
	$itt->assign("contract_page", contract_page($page, $pager->numPages, $pager->page_prev, $pager->page, $pager->page_next));
	$itt->assign("contract_display", $limit);
	$itt->assign("contract_page_end", $pager->numPages);
	$itt->assign("contract_page_curr", $pager->page);
	$itt->assign("contract_page_prev", $pager->page_prev);
	$itt->assign("contract_page_next", $pager->page_next);
	$itt->assign("search", $search);
	$itt->assign("tittle", "Quản lý hợp đồng");
	if($_REQUEST['get'] == 'table'){
		$itt->display('templates/contract_list_table.tpl');
	}
	else{
		$itt->display('templates/contract_list.tpl');
	}
}
elseif($do == 'add'){
	if(isset($_REQUEST['submit'])){
		$name = $_REQUEST['name'];
		$address = $_REQUEST['address'];
		$tax = $_REQUEST['tax'];
		$contract = $_REQUEST['contract'];
		$director = $_REQUEST['director'];
		$date = strtotime($_REQUEST['date']);
		$position = $_REQUEST['position'];
		$tamung = $_REQUEST['tamung'];
		$marketing = $_REQUEST['marketing'];
		$ngaygiao = $_REQUEST['ngaygiao'];
		$note = $_REQUEST['note'];
		$niemphong = $_REQUEST['niemphong'];
		$product_name = $_REQUEST['product_name'];
		$product_page = $_REQUEST['product_page'];
		$form = $_REQUEST['form'];
		$serial = $_REQUEST['serial'];
		$logo = $_REQUEST['logo'];
		$hoavan = $_REQUEST['hoavan'];
		$signed = $_REQUEST['signed'];
		$matma = $_REQUEST['matma'];
		$color = $_REQUEST['color'];
		$bowel = $_REQUEST['bowel'];
		$size = $_REQUEST['size'];
		$unit = $_REQUEST['unit'];
		$quantity = $_REQUEST['quantity'];
		$quantity_t = $_REQUEST['quantity_t'];
		$quantity_d = $_REQUEST['quantity_d'];
		$price = $_REQUEST['price'];
		if(strlen($tax) < 10){
			$itt->assign("error", "tax");
		}
		else{
			$cTamung = str_replace(',', '', $tamung);
			insertContract(array($name, $address, $tax, $contract, $director, $date, $position, $cTamung, $marketing, $ngaygiao, $note, $niemphong, 1));
			$cId = getContractId();
			foreach($product_page as $key => $p_page){
				$order = $key + 1;
				if($quantity_t[$key] == '' || $quantity_d[$key] == ''){
					$qty_t = 1;
					$qty_d = qty_d($unit[$key], $quantity[$key]);
				}
				else{
					$qty_t = $quantity_t[$key];
					$qty_d = $quantity_d[$key];
				}
				$sohuy = sohuy($p_page, $qty_t, $qty_d);
				$banhuy = banhuy($p_page, $color[$key]);
				$cPrice = str_replace(',', '', $price[$key]);
				$cQuantity = str_replace(',', '', $quantity[$key]);
				insertProduct(array($cId, $product_name[$key], $p_page, $form[$key], $serial[$key], $logo[$key], $hoavan[$key], $signed[$key], $matma[$key], $color[$key], $bowel[$key], $size[$key], $unit[$key], $cQuantity, $qty_t, $qty_d, $cPrice, $sohuy, $banhuy, $order));
			}
			updateContractProductNo($cId, $order);
		}
		$itt->assign("success", "ok");
	}
	else{
		$id = $_REQUEST['id'];
		$ocsql = mysql_query("SELECT * FROM `contract` WHERE `id` = '$id'");
		$oc = mysql_fetch_object($ocsql);
		$itt->assign("oc", $oc);
		$marketingsql = mysql_query("SELECT DISTINCT `marketing` FROM `contract`");
		while($rss = mysql_fetch_object($marketingsql)){
			$marketing_arr .= '~>'.$rss->marketing;
		}
		$marketing_arr = str2arr($marketing_arr);
		$itt->assign("marketing", $marketing_arr);
	}
	$itt->assign("newContractNumber", (getContractId()+1));
	$itt->assign("tittle", "Thêm mới hợp đồng");
	$itt->display('templates/contract_add.tpl');
}
elseif($do == 'statistic'){
	if(isset($_REQUEST['chon'])){
		$marketing = $_REQUEST['marketing']; //24/11/2011
		$tktungay = strtotime($_REQUEST['tktungay']) - 1;
		$tkdenngay = strtotime($_REQUEST['tkdenngay']) + 1;
		$select = mysql_query("SELECT * FROM `contract` WHERE `date` > '$tktungay' AND `date` < '$tkdenngay' AND `marketing` = '$marketing'") or die ('gọi Phương! Lỗi dòng số 22 file statistic.php');
		$marketing_sql = mysql_query("select distinct `marketing` from `contract`") or die ('gọi Phương! Lỗi dòng số 26 file statistic.php');
		$itt->assign("tktungay", date("d-m-Y", $tktungay));
		$itt->assign("tkdenngay", date("d-m-Y", $tkdenngay));
	}
	else
	{
		$select = mysql_query("select * from contract") or die ('gọi Phương! Lỗi dòng số 26 file statistic.php');
		$marketing_sql = mysql_query("select distinct `marketing` from `contract`") or die ('gọi Phương! Lỗi dòng số 26 file statistic.php');
	}
	while($rss = mysql_fetch_object($marketing_sql)){
		$marketing_arr .= '~>'.$rss->marketing;
	}
	$marketing_arr = str2arr($marketing_arr);
	$tktotalcty = mysql_num_rows($select);
	$tksubamount_v = 0;
	$tkvatamount_v = 0;
	$tktotalhd_v = 0;
	$tktotalkemhuy_v = 0;
	$tktotalgiayhuy_v = 0;
	$tktotalhd_1 = 0;
	$tktotalhd_2 = 0;
	//$rs = '';
	while($rs = mysql_fetch_object($select))
	{
		for($i=1;$i<=$rs->product;$i++)
		{
			$pr = mysql_fetch_object(mysql_query("SELECT * FROM `product` WHERE `cid` = '$rs->id' and `order` ='$i' ORDER BY `order` ASC"));
			$tksubamount_v = $tksubamount_v + ($pr->quantity * $pr->price);
			$tkvatamount_v = $tksubamount_v * 10 / 100;
			if($pr->unit=='quyen')
			{
				$tktotalhd_1 = $pr->quantity * 50;
			}
			else
			{
				$tktotalhd_1 = $pr->quantity;
			}
			$tktotalhd_v = $tktotalhd_v + $tktotalhd_1;
			$tktotalkemhuy_v = $tktotalkemhuy_v + $pr->banhuy;
			$tktotalgiayhuy_v = $tktotalgiayhuy_v + $pr->sohuy;
		}
	}
	$tksubamount = number_format($tksubamount_v);
	$tkvatamount = number_format($tkvatamount_v);
	$tktotalamount = number_format($tksubamount_v + $tkvatamount_v);
	$tktotalhd = number_format($tktotalhd_v);
	$tktotalkemhuy = number_format($tktotalkemhuy_v);
	$tktotalgiayhuy = number_format($tktotalgiayhuy_v);
	if(isset($_REQUEST['chitiet'])){
		if(isset($_REQUEST['tktungay'])){$tungay_chitiet = $_REQUEST['tktungay'];}else{$tungay_chitiet = '01-01-2010';}
		if(isset($_REQUEST['tkdenngay'])){$denngay_chitiet = $_REQUEST['tkdenngay'];}else{$denngay_chitiet = date('d-m-Y', time());}
		header("Location:export?filename=chitiet&tungay=$tungay_chitiet&denngay=$denngay_chitiet&marketing=$marketing");
	}
	
	$itt->assign("tktotalcty", $tktotalcty);
	$itt->assign("tksubamount", $tksubamount);
	$itt->assign("tkvatamount", $tkvatamount);
	$itt->assign("tktotalamount", $tktotalamount);
	$itt->assign("tktotalhd", $tktotalhd);
	$itt->assign("tktotalkemhuy", $tktotalkemhuy);
	$itt->assign("tktotalgiayhuy", $tktotalgiayhuy);
	$itt->assign("marketing_arr", $marketing_arr);
	$itt->assign("marketing", $marketing);
	$itt->assign("tittle", "Thống kê hợp đồng");
	$itt->display('templates/contract_statistic.tpl');
}
?>
